﻿using System;
using System.ComponentModel;
using System.Windows;

using MetaVideoEditorEvolved.Entity;
using MetaVideoEditorEvolved.ViewModel;
using Microsoft.Windows.Controls.Ribbon;
using mveEngine;

namespace MetaVideoEditorEvolved
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : RibbonWindow
	{
		private ViewModel.MainWindowVM vm;

		public MainWindow()
		{
			InitializeComponent();
			
			GalaSoft.MvvmLight.Messaging.Messenger.Default.Register(this, new System.Action<Messages.OpenWindowMessage>((args)=>
			                                                                                                            {
			                                                                                                            	var openWindow = args as Messages.OpenWindowMessage;
			                                                                                                            	
			                                                                                                            	if (openWindow.WindowToOpen == Messages.WindowType.GenreMappingWindow) {
			                                                                                                            		var mappingWindow = new MappingGenreWindow();
			                                                                                                            		mappingWindow.Owner = this;
			                                                                                                            		mappingWindow.DataContext = new ViewModel.MappingGenreWindowVM();
			                                                                                                            		mappingWindow.ShowDialog();
			                                                                                                            	}
			                                                                                                            }));
			
			this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
			this.Closing+= new CancelEventHandler(MainWindow_Closing);
		}

		void MainWindow_Closing(object sender, CancelEventArgs e)
		{
			if(Config.Instance.FullscreenMode &&
			   this.WindowState != WindowState.Minimized)
			{
				Config.Instance.WindowGeometryHeight = this.Height;
				Config.Instance.WindowGeometryWidth = this.Width;
				Config.Instance.WindowGeometryTop = this.Top;
				Config.Instance.WindowGeometryLeft = this.Left;
				Config.Instance.WindowGeometryState = this.WindowState;
			}
		}

		void MainWindow_Loaded(object sender, RoutedEventArgs e)
		{
			this.vm = new ViewModel.MainWindowVM();
			this.DataContext = this.vm;

			vm.LoadedCommand.Execute(null);

			System.Windows.Application.Current.MainWindow = this;
			
			if(Config.Instance.FullscreenMode)
			{
				this.Height = Config.Instance.WindowGeometryHeight;
				this.Width = Config.Instance.WindowGeometryWidth;
				this.Left = Config.Instance.WindowGeometryLeft;
				this.Top = Config.Instance.WindowGeometryTop;
				this.WindowState = Config.Instance.WindowGeometryState;
			}
			else
			{
				this.Height= 600.0;
				this.Width= 800.0;
				this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
				this.WindowState = WindowState.Maximized;
			}
		}

		private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
		{
			this.vm.SelectedUiMediaItem = this.TreeViewMediaItems.SelectedItem as MetaVideoEditorEvolved.UI.Entity.MediaItemBaseEntity;
			
			this.MediaTabControl.Items.Clear();
			this.MediaTabControl.Items.Add(this.TitleOvTab);
			this.MediaTabControl.Items.Add(this.TitleActTab);
			this.MediaTabControl.Items.Add(this.TitleCreTab);
			this.MediaTabControl.Items.Add(this.TitlePoTab);
			this.MediaTabControl.Items.Add(this.TitleBdTab);
			this.MediaTabControl.Items.Add(this.TitleBaTab);
			this.MediaTabControl.Items.Add(this.TitleTraTab);
			
			if(this.vm.SelectedUiMediaItem is UI.Entity.MovieMediaItem)
			{
				this.MediaTabControl.Items.Remove(this.TitleBaTab);
			}
			
			if (this.vm.SelectedUiMediaItem is UI.Entity.EpisodeMediaItem ||
			    this.vm.SelectedUiMediaItem is UI.Entity.FolderMediaItem) {
				this.MediaTabControl.Items.Remove(this.TitlePoTab);
				this.MediaTabControl.Items.Remove(this.TitleCreTab);
				this.MediaTabControl.Items.Remove(this.TitleBdTab);
				this.MediaTabControl.Items.Remove(this.TitleBaTab);
				this.MediaTabControl.Items.Remove(this.TitleTraTab);
				this.MediaTabControl.Items.Remove(this.TitleActTab);
			}
			
			if (this.vm.SelectedUiMediaItem is UI.Entity.SeasonMediaItem)
			{
				this.MediaTabControl.Items.Remove(this.TitleCreTab);
				this.MediaTabControl.Items.Remove(this.TitleBdTab);
				this.MediaTabControl.Items.Remove(this.TitleBaTab);
				this.MediaTabControl.Items.Remove(this.TitleTraTab);
				this.MediaTabControl.Items.Remove(this.TitleActTab);
			}
			
			if (this.vm.SelectedUiMediaItem is UI.Entity.SerieMediaItem)
			{
				this.MediaTabControl.Items.Remove(this.TitleCreTab);
				this.MediaTabControl.Items.Remove(this.TitleTraTab);
			}
		}

		void CheckBox_Checked(object sender, RoutedEventArgs e)
		{
			((UI.Entity.VideoMediaElement) this.vm.SelectedUiMediaItem).HasChanged = true;
		}
	}
}
